package com.wonders.framework.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wonders.framework.web.context.RequestContext;

/**
 * 全局过滤器
 * 
 * @author WuBin
 * 
 */
public class GlobalFilter implements Filter {

	public GlobalFilter() {
	}

	public void destroy() {
		if (RequestContext.get() != null) {
			RequestContext.get().end();// 清除线程变量值
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		RequestContext.begin(req, res);// 初始化线程变量值
		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}
}
